package com.apps.ftapp.adapter;

import android.content.Context;
import android.graphics.Bitmap;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.apps.ftapp.R;
import com.apps.ftapp.bean.ZhaopinBean;
import com.apps.ftapp.model.IndexModel1;
import com.nostra13.universalimageloader.core.DisplayImageOptions;
import com.nostra13.universalimageloader.core.ImageLoader;
import com.nostra13.universalimageloader.core.ImageLoaderConfiguration;

import java.util.List;

/**
 * Created by lyli on 2017/10/12.
 */

public class IndexGridBook2Adapter extends BaseAdapter {

    private Context mContext;
    List<ZhaopinBean> dataList;
    private ImageLoader imageLoader = ImageLoader.getInstance();
    private DisplayImageOptions options;

    public IndexGridBook2Adapter(Context mContext, List<ZhaopinBean> dataList) {
        super();
        this.mContext = mContext;
        this.dataList = dataList;
        //显示图片的配置  
        options = new DisplayImageOptions.Builder()
                .showImageOnLoading(R.mipmap.ic_launcher)
                .showImageOnFail(R.mipmap.ic_launcher).cacheInMemory(true).cacheOnDisk(true).bitmapConfig(Bitmap.Config.RGB_565).build();
        imageLoader.init(ImageLoaderConfiguration.createDefault(mContext));
    }

    @Override
    public int getCount() {
        if (dataList == null) {
            return 0;
        } else {
            return this.dataList.size();
        }
    }

    @Override
    public Object getItem(int i) {
        if (dataList == null) {
            return null;
        } else {
            return this.dataList.get(i);
        }
    }

    @Override
    public long getItemId(int i) {
        return i;
    }

    @Override
    public View getView(int i, View view, ViewGroup viewGroup) {
        ZhaopinBean zp = dataList.get(i);
        ViewHolder holder = null;
        if (view == null) {
            view = LayoutInflater.from(this.mContext).inflate(R.layout.fragment_index_listview_zigezheng_gridview_item, null, false);
            holder = new ViewHolder(view);
            view.setTag(holder);
        } else {
            holder = (ViewHolder) view.getTag();
        }
        holder.price.setText("¥" + zp.getPrice());
        holder.saleNum.setText("已购：" + zp.getStudentNum());

        imageLoader.displayImage(zp.getLargePicture(), holder.bookImg, options);


        return view;
    }


    private class ViewHolder {

        ImageView bookImg;
        TextView price;
        TextView saleNum;

        public ViewHolder(View view) {
            bookImg = view.findViewById(R.id.book_img);
            price = view.findViewById(R.id.price);
            saleNum = view.findViewById(R.id.sale_num);
        }
    }

}
